package com.ruoyi.web.controller.system;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.system.domain.AppUser;
import com.ruoyi.system.service.IAppUserService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.BaseFriendsDefault;
import com.ruoyi.system.service.IBaseFriendsDefaultService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 默认好友Controller
 *
 * @author ruoyi
 * @date 2023-03-08
 */
@RestController
@RequestMapping("/system/friendDefault")
public class BaseFriendsDefaultController extends BaseController {
    @Autowired
    private IBaseFriendsDefaultService baseFriendsDefaultService;
    @Autowired
    private IAppUserService iAppUserService;

    /**
     * 查询默认好友列表
     */
    @PreAuthorize("@ss.hasPermi('system:friendDefault:list')")
    @GetMapping("/list")
    public TableDataInfo list(BaseFriendsDefault baseFriendsDefault) {
        startPage();
        List<BaseFriendsDefault> list = baseFriendsDefaultService.selectBaseFriendsDefaultList(baseFriendsDefault);
        return getDataTable(list);
    }

    /**
     * 导出默认好友列表
     */
    @PreAuthorize("@ss.hasPermi('system:friendDefault:export')")
    @Log(title = "默认好友", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, BaseFriendsDefault baseFriendsDefault) {
        List<BaseFriendsDefault> list = baseFriendsDefaultService.selectBaseFriendsDefaultList(baseFriendsDefault);
        ExcelUtil<BaseFriendsDefault> util = new ExcelUtil<BaseFriendsDefault>(BaseFriendsDefault.class);
        util.exportExcel(response, list, "默认好友数据");
    }

    /**
     * 获取默认好友详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:friendDefault:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(baseFriendsDefaultService.selectBaseFriendsDefaultById(id));
    }

    /**
     * 新增默认好友
     */
    @PreAuthorize("@ss.hasPermi('system:friendDefault:add')")
    @Log(title = "默认好友", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BaseFriendsDefault baseFriendsDefault) {
        AppUser appUser = iAppUserService.getOne(new QueryWrapper<AppUser>().eq("account", baseFriendsDefault.getAccount()));
        if (appUser == null) {
            return error("用户不存在");
        }
        baseFriendsDefault.setNickName(appUser.getUsername());

        Long count = baseFriendsDefaultService.count(new QueryWrapper<BaseFriendsDefault>().eq("account", baseFriendsDefault.getAccount()));
        if (count > 0) {
            return error("默认好友已存在");
        }
        return toAjax(baseFriendsDefaultService.insertBaseFriendsDefault(baseFriendsDefault));
    }

    /**
     * 修改默认好友
     */
    @PreAuthorize("@ss.hasPermi('system:friendDefault:edit')")
    @Log(title = "默认好友", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BaseFriendsDefault baseFriendsDefault) {
        AppUser appUser = iAppUserService.getOne(new QueryWrapper<AppUser>().eq("account", baseFriendsDefault.getAccount()));
        if (appUser == null) {
            return error("用户不存在");
        }
        baseFriendsDefault.setNickName(appUser.getUsername());

        Long count = baseFriendsDefaultService.count(new QueryWrapper<BaseFriendsDefault>()
                .eq("account", baseFriendsDefault.getAccount()).ne("id",baseFriendsDefault.getId())
        );
        if (count > 0) {
            return error("默认好友已存在");
        }

        return toAjax(baseFriendsDefaultService.updateBaseFriendsDefault(baseFriendsDefault));
    }

    /**
     * 删除默认好友
     */
    @PreAuthorize("@ss.hasPermi('system:friendDefault:remove')")
    @Log(title = "默认好友", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(baseFriendsDefaultService.deleteBaseFriendsDefaultByIds(ids));
    }
}
